import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/components/Layout'
import P404 from '@/components/P404'
import P403 from '@/components/P403'
import Login from '@/views/user/Login'

import systemRouter from './router.system'
import personnelRouter from './router.personnel'
import templateRouter from './router.template'
import productRouter from './router.product'
import productDataRouter from './router.productData'
import supplierRouter from './router.supplier'
import businessRouter from './router.business'
import qpProductDataRouter from './router.qpProductData'
import financialRouter from './router.financial'
import personal from './router.personal'

Vue.use(Router)

export const constantRoutes = [{
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
        disablePermission: true
    }
}, {
    path: '/',
    component: Layout,
    children: [
        ...financialRouter,
        ...systemRouter,
        ...personnelRouter,
        ...templateRouter,
        ...productRouter,
        ...productDataRouter,
        ...supplierRouter,
        ...businessRouter,
        ...qpProductDataRouter,
        ...personal,
        {
            path: '*',
            component: P404,
            meta: {
                label: '页面不存在',
                disablePermission: true
            }
        }, {
            path: 'unauthorized',
            name: 'P403',
            component: P403,
            meta: {
                label: '未授权页面',
                disablePermission: true
            }
        }
    ]
}]

const createRouter = () => new Router({
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router